package shuashuami.hb.com.avtivity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.LoginHttp;
import shuashuami.hb.com.share.ShareCUserInfo;
import shuashuami.hb.com.util.DataCleanManager;

public class ASettingActivity extends AbActivity {

    private LinearLayout llCHangePassword, llYOnghuXieyi, llCLear;


    private TextView tvHuanCun;

    private Button loginOut;

    private TextView tvIsNew;


    @Override
    protected void initUI() {
        setAbContentView(R.layout.activity_csetting);
        llCHangePassword = (LinearLayout) findViewById(R.id.ll_setting_change_password);
        llYOnghuXieyi = (LinearLayout) findViewById(R.id.ll_setting_yonghuxieyi);
        llCLear = (LinearLayout) findViewById(R.id.ll_setting_clear);
        tvHuanCun = (TextView) findViewById(R.id.tv_setting_huancun);
        tvIsNew  =(TextView) findViewById(R.id.tv_isnew);
        loginOut = (Button) findViewById(R.id.btn_login_out);
    }

    @Override
    protected void initData() {
        setTitleBar("个人设置");
        setLeftView();
        try {
            tvHuanCun.setText(DataCleanManager.getTotalCacheSize(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initListener() {
        llCHangePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(AChangePasswordActivity.class);
            }
        });
        llYOnghuXieyi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(YongHuXieyiActivity.class);
            }
        });

        llCLear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvHuanCun.setText("0.0k");
                ToastUtil.showShort(getApplication(), "已清除");
                DataCleanManager.clearAllCache(ASettingActivity.this);
            }
        });
        loginOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Platform qzone = ShareSDK.getPlatform(Wechat.NAME);
                if (qzone.isAuthValid()) {
                    qzone.removeAccount(true);
                }
//                qzone.setPlatformActionListener(new PlatformActionListener() {
//                    @Override
//                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
//
//                    }
//
//                    @Override
//                    public void onError(Platform platform, int i, Throwable throwable) {
//
//                    }
//
//                    @Override
//                    public void onCancel(Platform platform, int i) {
//
//                    }
//                });
//                qzone.authorize();
//                qzone.showUser(null);
                clear();
                openActivity(HbLoginAct.class);
                finishAllWithOutLast();
                finish();


            }
        });


    }

    private void clear() {
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(this);
        shareCUserInfo.clear();
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    String result = msg.obj.toString();
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(ASettingActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            ToastUtil.showShort(ASettingActivity.this, jsonObject.getString("message"));
                        } else {
                            ToastUtil.showShort(ASettingActivity.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    String results = msg.obj.toString();
                    try {
                        JSONObject jsonObject = new JSONObject(results);
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(ASettingActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 405) {
                            tvIsNew.setVisibility(View.VISIBLE);
                            ToastUtil.showShort(ASettingActivity.this, jsonObject.getString("message"));
                        } else {
                            ToastUtil.showShort(ASettingActivity.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }

            super.handleMessage(msg);
        }
    };
}
